const {
  honorDetailApi,
  caseInfoDetailApi,
  fileGetAliOssUrlPathApi,
  caseInfoGetListApi,
  honorGetListApi,
  mobileKeyGetMobileKeyApi,
  caseInfoVerifyAccessRightApi
} = require('../../api/index')

const {
  dealContent
} = require('../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    contentId: '',
    contentType: '' as any,
    pageTitle: "",
    isLimit: false,
    isUse: false,
    limitVisable: false,
    formData: {
      phone: "",
      code: ""
    },
    timeNumber: 0,
    timer: null as any,
    caseTimer: null as any,
  },
  closeLimitVisable() {
    this.setData({
      limitVisable: false
    })
    wx.navigateBack({ delta: -1 })
  },
  bindKeyInput(e) {
    this.setData({
      formData: {
        ...this.data.formData,
        [e.currentTarget.dataset.filed]: e.detail.value
      }
    })
  },
  getCode() {
    if (!/^1[3-9]\d{9}$/.test(this.data.formData.phone)) {
      wx.showToast({
        icon: "none",
        title: "手机号格式不正确"
      })
      return
    }

    if (this.data.timeNumber !== 0) {
      wx.showToast({
        icon: "none",
        title: `请${this.data.timeNumber}s后再获取验证码`
      })
      return
    }
    mobileKeyGetMobileKeyApi({
      dataId: this.data.contentId,
      msgType: 1,
      userMobile: this.data.formData.phone
    }).then(res => {

      if (res.code == 200) {
        this.setData({
          isUse: false,
          timeNumber: 60
        })
        const intervalId = setInterval(() => {
          this.setData({
            timeNumber: this.data.timeNumber - 1
          })
          if (this.data.timeNumber === 0) {
            clearInterval(intervalId)
          }
        }, 1000)
        this.setData({
          timer: intervalId
        })
      } else {
        this.setData({
          isUse: true
        })
        wx.showToast({
          icon: 'none',
          title: res.data.msg
        })
      }
    })
  },

  getVerifyAccess() {
    if (!/^1[3-9]\d{9}$/.test(this.data.formData.phone)) {
      wx.showToast({
        icon: "none",
        title: "手机号格式不正确"
      })
      return
    }
    if (!this.data.formData.code) {
      wx.showToast({
        icon: "none",
        title: "请输入验证码"
      })
      return
    }
    caseInfoVerifyAccessRightApi({
      caseInfoId: this.data.contentId,
      smsCode: this.data.formData.code,
      userMobile: this.data.formData.phone
    }).then(res => {
      if (res.code == 200) {
        this.getListFun()
        this.setData({
          isLimit: false,
          limitVisable: false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(optios) {
    this.setPageTitle(optios.type)
    this.setData({
      contentId: optios.id,
      contentType: optios.type
    })
  },
  setPageTitle(contentType: any) {
    let title = ""
    switch (contentType) {
      case "1":
        title = '经典案例'
        break
      case "2":
        title = '荣誉记录'
        break
    }
    this.setData({
      pageTitle: title
    })
    wx.setNavigationBarTitle({ title: title + '详情' })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getListFun()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (this.data.timer) {
      clearInterval(this.data.timer)
    }

    if (this.data.caseTimer) {
      clearInterval(this.data.caseTimer)
    }

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  getListFun() {
    if (this.data.contentType == 1) {
      Promise.all([
        fileGetAliOssUrlPathApi(),
        caseInfoDetailApi({
          caseInfoId: this.data.contentId
        }),
        caseInfoGetListApi({
          pageNume: 1,
          pageSize: 5
        })
      ]).then(res => {
        wx.hideNavigationBarLoading()
        if (res[1].code == 405) {
          this.setData({
            isLimit: true,
            limitVisable: true
          })
          return
        }
        res[1].data.leadLawyers.forEach(t => {
          t.isLead = true
        });
        res[1].data.content = dealContent(res[1].data.content, res[0].data.aliOssUrlPath)
        res[1].data.lawyerList = [
          ...res[1].data.leadLawyers,
          ...res[1].data.caseLawyers
        ]
        res[1].data.time = res[1].data.closeTime

        this.setData({
          contentInfo: res[1].data,
          dataList: res[2].data.list
        })

        if (res[1].data.limitOrNot) {
          const intervalId = setInterval(() => {
            caseInfoDetailApi({
              caseInfoId: this.data.contentId
            }).then(res => {
              if (res.code == 405) {
                this.setData({
                  isLimit: true,
                  limitVisable: true
                })
                return
              }
            })
          }, 1000 * 15)

          this.setData({
            caseTimer: intervalId
          })
        }
      })
    }

    if (this.data.contentType == 2) {
      Promise.all([
        fileGetAliOssUrlPathApi(),
        honorDetailApi({
          honorId: this.data.contentId
        }),
        honorGetListApi({
          pageSise: 5,
          pageNum: 1
        })
      ]).then(res => {
        wx.hideNavigationBarLoading()
        res[1].data.content = dealContent(res[1].data.content, res[0].data.aliOssUrlPath)
        res[1].data.time = res[1].data.honorTime
        res[1].data.lawyerList = res[1].data.lawyers

        this.setData({
          contentInfo: res[1].data,
          dataList: res[2].data.list
        })
      })
    }

  }
})